package cn.com.czpb.client.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.File;
import java.util.UUID;

public class DeviceUtils {

    /**
     * 判断手机是否ROOT
     */
    public static boolean isRoot() {

        boolean root = false;

        try {
            if ((!new File("/system/bin/su").exists())
                    && (!new File("/system/xbin/su").exists())) {
                root = false;
            } else {
                root = true;
            }

        } catch (Exception e) {
        }

        return root;
    }
    public static String getMyUUID(Context context) {

        final SharedPreferences prefs = context.getSharedPreferences("device", 0);
        String deviceId = prefs.getString("device_id", null);
        if (TextUtils.isEmpty(deviceId)) {
            final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            final String tmDevice, tmSerial, androidId;
            tmDevice = "" + tm.getDeviceId() + tm.getSubscriberId();// the IMEI
            // for GSM
            // and the
            // MEID or
            // ESN for
            // CDMA
            // phones.
            tmSerial = "" + tm.getSimSerialNumber();
            androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);

            LogUtils.i("tmDevice"+ tmDevice);
            LogUtils.i("imei" + tm.getDeviceId());
            LogUtils.i("imsi" + tm.getSubscriberId());
            LogUtils.i("androidId"+ androidId);
            LogUtils.i("Build.PRODUCT: "+ Build.PRODUCT);
            LogUtils.i("Build.SERIAL: "+Build.SERIAL);

            boolean inEmulator = ("000000000000000".equals(tm.getDeviceId()) && null != tm.getDeviceId())
                    || TextUtils.isEmpty(androidId) || "google_sdk".equals(Build.PRODUCT)
                    || "sdk".equals(Build.PRODUCT);

            UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
            deviceId = inEmulator ? "emulator:" + deviceUuid.toString() : deviceUuid.toString();
            prefs.edit().putString("device_id", deviceId).commit();
        }

        return deviceId;

    }
    /**
     * 手机对应的是IMEIdeviceid
     * IMEI（International Mobile Equipment Identity）是移动设备国际身份码的缩写，
     * 移动装备国际辨识码，是由15位数字组成的"电子串号"，它与每台手机一一对应，而且该码是全世界唯一的。
     *
     * @return
     */
    public static String getIMEI(Context context) {
        TelephonyManager systemService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = systemService.getDeviceId();
        return imei;
    }

    /**
     * IMSI是相对手机卡而言的
     * 国际移动用户识别码（IMSI：International Mobile Subscriber Identification Number）
     * @param context
     * @return
     */
    public static String getIMSI(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getSubscriberId();
    }

    /**
     * 手机号
     * @param context
     * @return
     */
    public static String getLine1Number(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getLine1Number();
    }


    /**
     * 返回设备型号
     * @param mContext
     * @return
     */

    public static String getDeviceInfo(Context mContext) {
        TelephonyManager tm = (TelephonyManager) mContext
                .getSystemService(Context.TELEPHONY_SERVICE);
        String deviceInfo = Build.MODEL;
        return deviceInfo;
    }

    /**
     * 获取androidManifest.xml上配置的渠道信息
     *
     * @param context
     * @param name
     * @return
     */
    public static String getChannelValue(Context context, String name) {
        PackageManager packageManager = context.getPackageManager();
        String msg = null;
        try {
            ApplicationInfo appInfo = packageManager.getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            msg = appInfo.metaData.getString(name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static int getChannelCode(Context context) {
        String channelValue = getChannelValue(context, "UMENG_CHANNEL");
        int channelCode = 1000;
        if(TextUtils.isEmpty(channelValue)){
            return channelCode;
        }
        try{
            channelCode = Integer.valueOf(channelValue);
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        /*switch (channelValue) {
            case "fazhiwang":
                channelCode = 1000;
                break;
            case "yingyongbao":
                channelCode = 1002;
                break;
            case "androidguanwang":
                channelCode = 1003;
                break;
            case "market360":
                channelCode = 1004;
                break;
            default:
                break;
        }*/
        return channelCode;
    }
    public static float dp2px(Resources resources, float dp) {
        final float scale = resources.getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    public static float sp2px(Resources resources, float sp) {
        final float scale = resources.getDisplayMetrics().scaledDensity;
        return sp * scale;
    }
    public static float px2dip(Resources resources, int px) {
        final float scale = resources.getDisplayMetrics().scaledDensity;
        return px / scale + 0.5f;
    }
}
